import 'package:aitao_flutter/common_ui/age_widget.dart';
import 'package:aitao_flutter/common_ui/at_switch.dart';
import 'package:aitao_flutter/const/common_const.dart';
import 'package:aitao_flutter/style/box_style.dart';
import 'package:aitao_flutter/style/color_style.dart';
import 'package:aitao_flutter/style/edgeInsets_style.dart';
import 'package:aitao_flutter/style/radius_style.dart';
import 'package:aitao_flutter/style/text_style.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../common_ui/circle_avatar.dart';
import '../../res/r.dart';
import '../controller/mine_page_controller.dart';

class MinePage extends StatelessWidget {
  MinePage({Key? key}) : super(key: key);

  final MinePageController controller = Get.put(MinePageController());
  @override
  Widget build(BuildContext context) {
    return controller.userData == null
        ? const Center(
            child: Text('加载中...'),
          )
        : Scaffold(
            backgroundColor: ColorStyle.whiteColor,
            // body: CustomScrollView(
            //   slivers: [
            //     GetBuilder<MinePageController>(builder: ((controller) {
            //       return _headerWidget();
            //     })),
            //     GetBuilder<MinePageController>(builder: ((controller) {
            //       return _menuList();
            //     })),
            //     GetBuilder<MinePageController>(builder: ((controller) {
            //       return _tabList();
            //     })),
            //   ],
            // ),
            body: GetBuilder<MinePageController>(builder: ((controller) {
              return CustomScrollView(
                slivers: [_headerWidget(), _menuList(), _tabList()],
              );
            })));
  }

  Widget _menuList() {
    return SliverToBoxAdapter(
        child: Container(
      margin: EdgeInsetsStyle.edgeInsets(20, 20),
      alignment: Alignment.center,
      color: ColorStyle.whiteColor,
      child: Wrap(
          spacing: 36,
          runSpacing: 15,
          children: controller.menuListData.map((e) {
            return _menuItem(e['title'].toString(), e['img'].toString());
          }).toList()),
    ));
  }

  Widget _menuItem(String title, String img) {
    return GestureDetector(
        onTap: () {
          controller.routerClick(title);
        },
        child: Column(
          children: [
            Image.asset(
              img,
              width: 50.w,
              height: 50.w,
            ),
            Text(title)
          ],
        ));
  }

  Widget _tabList() {
    return SliverList.separated(
      itemBuilder: (context, index) {
        Map map = controller.tabListData[index];
        return GestureDetector(
          child: _tabItem(map['img'], map['title']),
          onTap: () {
            controller.routerClick(map['title']);
          },
        );
      },
      itemCount: controller.tabListData.length,
      separatorBuilder: (context, index) {
        return const Divider(
          thickness: 1,
          color: ColorStyle.f3f3f3Color,
        );
      },
    );
  }

  Widget _tabItem(String img, String title) {
    return Container(
      color: ColorStyle.whiteColor,
      padding: EdgeInsetsStyle.edgeInsets(16, 11),
      // padding: EdgeInsets.fromLTRB(16, 0, 16, 0),
      height: 42.h,
      child: Row(children: [
        Image.asset(img),
        Box.hBox15,
        Text(
          title,
          style: Styles.normalText_14,
        ),
        const Spacer(),
        (title == '视频勿扰' || title == '消息勿扰')
            ? CommonSwitchWidget(
                isSelect: true,
                switchCallBack: (e) {
                  debugPrint('e:$e');
                })
            : const Icon(Icons.keyboard_arrow_right_rounded)
      ]),
    );
  }

  Widget _headerWidget() {
    return SliverToBoxAdapter(
        child: Container(
      padding: EdgeInsetsStyle.customMargin(20, 35, 20, 20),
      // width: MediaQuery.of(Get.context!).size.width,
      decoration: const BoxDecoration(
        color: ColorStyle.whiteColor,
        image: DecorationImage(
          image: AssetImage(ImagePath.assetsImagesMineTopBG),
          fit: BoxFit.fill, // 完全填充
        ),
      ),
      child: Column(children: [
        Row(
          children: [
            _avatarWidget(),
            Expanded(
                child: Column(
              children: [
                Row(
                  children: [
                    _vipIconWidget(),
                    Box.hBox3,
                    _userNameWidget(),
                    Box.hBox6,
                    _ageWidget(),
                    Box.hBox6,
                    _idWidget(),
                  ],
                ),
                Box.vBox15,
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                  children: [
                    _countWidget(
                        controller.userData?.usercount?.followCount ?? 0,
                        MineStringConst.focusText),
                    _countWidget(controller.userData?.usercount?.fansCount ?? 0,
                        MineStringConst.fansText),
                    _countWidget(
                        controller.friendCount, MineStringConst.friendText),
                    _countWidget(
                        controller.userData?.usercount?.visitorCount ?? 0,
                        MineStringConst.seeMeText)
                  ],
                )
              ],
            )),
            // const Spacer(),
            _settingIcon()
          ],
        ),
        Box.vBox20,
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            GestureDetector(
              child: _leftBannerWidget(),
              onTap: () {
                controller.routerClick(MineStringConst.rechargeText);
              },
            ),
            GestureDetector(
              child: _rightBannerWidget(),
              onTap: () {
                controller.routerClick(MineStringConst.memberText);
              },
            )
          ],
        )
      ]),
    ));
  }

  Widget _leftBannerWidget() {
    return Stack(
      alignment: Alignment.center,
      children: [
        Image.asset(
          ImagePath.assetsImagesMineLeft,
          fit: BoxFit.fill,
          height: 94.w,
        ),
        Positioned(
            top: 15,
            child: Column(
              children: [
                Text(
                  '我的${CommonStringConst.manCoinName}',
                  style: Styles.whiteText_13,
                ),
                Text(
                  '${controller.coin}${CommonStringConst.manCoinName}',
                  style: Styles.whiteText_13,
                )
              ],
            )),
        Positioned(
            bottom: 9,
            child: Container(
              width: 70.w,
              height: 22.w,
              decoration: BoxDecoration(
                  borderRadius: RadiusStyle.radiusAll(11.w),
                  color: ColorStyle.whiteColor),
              alignment: Alignment.center,
              child: Text(
                '去充值',
                style: Styles.mainText_12,
              ),
            ))
      ],
    );
  }

  Widget _rightBannerWidget() {
    return Stack(
      alignment: Alignment.center,
      children: [
        Image.asset(
          ImagePath.assetsImagesMineRight,
          fit: BoxFit.fill,
          height: 94.w,
        ),
        Positioned(
            top: 15,
            child: Column(
              children: [
                Text(
                  '购买会员,免费聊天和',
                  style: Styles.whiteText_13,
                ),
                Text(
                  '视频打折,多重优惠',
                  style: Styles.whiteText_13,
                )
              ],
            )),
        Positioned(
            bottom: 9,
            child: Container(
              width: 70.w,
              height: 22.w,
              decoration: BoxDecoration(
                  borderRadius: RadiusStyle.radiusAll(11.w),
                  color: ColorStyle.whiteColor),
              alignment: Alignment.center,
              child: Text(
                '去购买',
                style: Styles.customStyle(
                    const Color.fromRGBO(255, 126, 0, 1), 13),
              ),
            ))
      ],
    );
  }

  Widget _avatarWidget() {
    return GestureDetector(
      child: CircleAvatarWidget(
        imgHeight: 60,
        imgWidth: 60,
        url: controller.userData?.userinfo?.avatar ?? '',
        gender: controller.userData?.userinfo?.gender ?? 1,
      ),
      onTap: () {
        controller.routerClick('相册');
      },
    );
  }

  Widget _vipIconWidget() {
    return controller.getVipLevels() == ''
        ? Container(
            margin: const EdgeInsets.only(left: 10),
          )
        : Image.asset(
            controller.getVipLevels(),
            width: 30.w,
            height: 30.w,
          );
  }

  Widget _userNameWidget() {
    return ConstrainedBox(
        constraints: const BoxConstraints(maxWidth: 100),
        child: Text(
          controller.userData?.userinfo?.nickname ?? '',
          // style: Styles.normalText_18,
          style: const TextStyle(
              fontSize: 18,
              color: ColorStyle.normalTextColor,
              overflow: TextOverflow.ellipsis),
        ));
  }

  Widget _ageWidget() {
    return AgeWidget(
        age: controller.userData?.userinfo?.age ?? 18,
        gender: controller.userData?.userinfo?.gender ?? 1);
  }

  Widget _idWidget() {
    return Text(
      'ID: ${controller.userData?.userinfo?.usercode ?? ''}',
      style: Styles.greyText_13,
    );
  }

  Widget _settingIcon() {
    return GestureDetector(
      child: Image.asset(
        ImagePath.assetsImagesSettingIcon,
        width: 18.w,
        height: 18.w,
      ),
      onTap: () {
        controller.routerClick(MineStringConst.photoAlbumText);
      },
    );
  }

  Widget _countWidget(int count, String subTitle) {
    return GestureDetector(
      child: Column(
        children: [
          Text(
            count.toString(),
            style: Styles.normalText_13,
          ),
          Box.vBox3,
          Text(
            subTitle,
            style: Styles.greyText_14,
          ),
        ],
      ),
      onTap: () {
        controller.routerClick(subTitle);
      },
    );
  }
}
